/**
 * 
 */
package com.geekteam.masterenglish.view;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geekteam.masterenglish.R;
import com.geekteam.masterenglish.Adapter.AlbumAdapter;
import com.geekteam.masterenglish.provider.EEContract.Album;
import com.geekteam.masterenglish.provider.EEContract.Lession;

/**
 * @author MTROL
 *
 */
public class ItemListView extends RelativeLayout{
	private ImageView mCoverLession;
	private TextView mTitleLession;
	private TextView mAlbumLession;
	private TextView mNumberListen;
	private TextView mNumberFavorite;
	
	public ItemListView(Context context) {
		super(context);
		initView();
	}

	private void initView(){
		ListView.LayoutParams param = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT);
		this.setLayoutParams(param);
		LayoutInflater.from(getContext()).inflate(R.layout.item_lession, this, true);
		
		mCoverLession = (ImageView) findViewById(R.id.cover_lession);
		mTitleLession = (TextView) findViewById(R.id.title_lession);
		mAlbumLession = (TextView) findViewById(R.id.album_lession);
		mNumberFavorite = (TextView) findViewById(R.id.number_favorite);
		mNumberListen = (TextView) findViewById(R.id.number_listen);
	}
	
	public void updateView(Cursor cursor, int type){
		if(type == AlbumAdapter.TYPE_ALBUM){
			updateAlbumView(cursor);
		}else if(type==AlbumAdapter.TYPE_LESSION){
			updateLessionView(cursor);
		}
	}
	
	private void updateAlbumView(Cursor cursor){
		String title = cursor.getString(cursor.getColumnIndex(Album.TITLE));
		String albumId = cursor.getString(cursor.getColumnIndex(Album.BOOK_ID));
		int numberListen = cursor.getInt(cursor.getColumnIndex(Album.TOTAL_LISTEN));
		mTitleLession.setText(title);
		mAlbumLession.setText(albumId);
		mNumberListen.setText(""+numberListen);
		mNumberFavorite.setText(""+numberListen);
	}
	
	private void updateLessionView(Cursor cursor){
		String title = cursor.getString(cursor.getColumnIndex(Lession.TITLE));
		String albumId = cursor.getString(cursor.getColumnIndex(Lession.ALBUM_ID));
		int numberListen = cursor.getInt(cursor.getColumnIndex(Lession.TOTAL_LISTEN));
		mTitleLession.setText(title);
		mAlbumLession.setText(albumId);
		mNumberListen.setText(""+numberListen);
		mNumberFavorite.setText(""+numberListen);
	}
}
